package com.huluobo.a2109mouthdemo.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huluobo.a2109mouthdemo.MainActivity;
import com.huluobo.a2109mouthdemo.R;
import com.huluobo.a2109mouthdemo.base.BaseActivity;
import com.huluobo.a2109mouthdemo.register.RegisterActivity;
import com.hyphenate.chat.EMClient;

import org.w3c.dom.Text;

public class LoginActivity extends BaseActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {

    private EditText username, password;
    private Button toLogin;
    private TextView toRegister;

    @Override
    protected void initData() {
        presenter = new LoginPresenter();
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);
        toLogin = findViewById(R.id.login_to_login);
        toRegister = findViewById(R.id.login_to_register);

        toLogin.setOnClickListener(v -> {
            presenter.login(username.getText().toString(), password.getText().toString());
        });

        toRegister.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onUsernameError() {
        username.setError("用户名输入错误,需要首字符为英文,3-20位");
    }

    @Override
    public void onPasswordError() {
        password.setError("密码输入错误,需要3-20位数字");
    }

    @Override
    public void onStartLogin() {
        showProgressDialog("正在登录");
    }

    @Override
    public void onLoginSuccess() {
        dismissProgressDialog();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onLoginFailed(String error) {
        dismissProgressDialog();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LoginActivity.this, "登录失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }
}